﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Communication;
using Communication.Common;
using Communication.Udp;
using System.Net;
using System.Threading;

namespace Communication.Test
{
    class Program
    {
        private static IPEndPoint ip = null;
        static void Main(string[] args)
        {
            Console.WriteLine("please choose mode[s:sever,c:client]");
            String cmd = Console.ReadLine();
            if (cmd == "s")
            {
                Server();
            }
            else
            {
                
                Client();
            }
        }

        static void Server()
        {
            UdpServer server = new UdpServer();
            server.Port = 9999;
            server.StartReceive();
            server.StartSend();

            Thread t = new Thread(ServerReceive);
            t.IsBackground = true;
            t.Start();
            string str;
            EventMessage em = new EventMessage();
            Console.WriteLine("wil..");
 while ((str = Console.ReadLine()) != "\r\n" && ip != null)
            {
                em.Message = new Message() { MessageType = MessageType.ReqRoomIn, Data = str };
                em.IPEndPoints = new IPEndPoint[] { ip };
                EventMessageManager.CurrentEventMessageManager.AddSentEventMessage(em);
            }
 Console.WriteLine("...");
 Console.ReadLine();
        }

        static void ServerReceive()
        {
            EventMessage em;
            while (true)
            {
                em = EventMessageManager.CurrentEventMessageManager.GetReceivedEventMessage();
                if (em != null)
                {
                    Console.WriteLine("receive:"+(string)em.Message.Data);
                    ip = em.IPEndPoints[0];
                }
                else
                {
                    Thread.Sleep(500);
                }
            }
        }


        static void Client()
        {

            Console.WriteLine("Please input address");
            string strip = Console.ReadLine();
            Console.WriteLine("Please input port:");
            int port = int.Parse(Console.ReadLine());
            IPAddress[] addresses = Dns.GetHostAddresses(strip);
            IPAddress address = null;
            foreach (var ii in addresses)
            {
                if (ii.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    address = ii;
            }
            Console.WriteLine(address);
            Console.WriteLine(port);
            ip = new IPEndPoint(address, port);

            UdpServer server = new UdpServer();
            //server.Port = 9999;
            //server.StartReceive();
            server.StartSend();

            

            Thread t = new Thread(ServerReceive);
            t.IsBackground = true;
            t.Start();
            string str;
            EventMessage em = new EventMessage();
            bool send = false;
            while ((str = Console.ReadLine()) != "\r\n" && ip != null)
            {
                em.Message = new Message() { MessageType = MessageType.ReqRoomIn, Data = str };
                em.IPEndPoints = new IPEndPoint[] { ip };
                EventMessageManager.CurrentEventMessageManager.AddSentEventMessage(em);
                send = true;
                if (send)
                    server.StartReceive();
            }
        }

       
    }
}
